#include "stdio.h"
#include "unistd.h"
#include "mediactrl.h"

#define AW_WIDTH 1280
#define AW_HEIGH 720
#define AW_FPS 18
#define AW_BITRATE 2*1024*1024  //2Mbps

FILE *h264_fp;


uint64_t get_time(){

    struct timeval tv;
    gettimeofday( &tv, NULL );
    return tv.tv_sec*1000+tv.tv_usec/1000;
}

int32_t get_fps(){
    static uint64_t first_time = 0,last_time = 0;
    static int32_t cnt = 0;
    first_time = get_time();
    cnt++;
    if( first_time - last_time > 1000){
        printf( "fps = %d\r\n", cnt );
        cnt = 0;
        last_time = first_time;
    }
    return 0;
}

int32_t encCallback( VidEncOutputBuf *venc_out_buf, void *ctx ){
    if( venc_out_buf->key_flag == 1 ){
        fwrite(venc_out_buf->spspps_data, venc_out_buf->spspps_data_size, 1, h264_fp);
    }
    fwrite(venc_out_buf->data, venc_out_buf->data_size, 1, h264_fp);
    get_fps();
    return 0;
}

int main(){

    h264_fp = fopen( "/mnt/UDISK/test.h264", "w+" );

    aw_mediactrl_init( AW_WIDTH, AW_HEIGH, AW_FPS, AW_BITRATE );
    aw_mediactrl_regcb( encCallback, NULL );
    aw_mediactrl_start();

    while(1){

    }
    aw_mediactrl_stop();
    aw_mediactrl_deinit();
    fclose(h264_fp);

    return 0;
}